<?php

function sortTeamSPPsCompletions ($a, $b) {
  if ($b->completions == $a->completions) {
    return 0;
  } else {
    return $b->completions < $a->completions ? -1 : 1;
  }
}

function sortTeamSPPsTouchdowns ($a, $b) {
  if ($b->touchdowns == $a->touchdowns) {
    return 0;
  } else {
    return $b->touchdowns < $a->touchdowns ? -1 : 1;
  }
}

function sortTeamSPPsInterceptions ($a, $b) {
  if ($b->interceptions == $a->interceptions) {
    return 0;
  } else {
    return $b->interceptions < $a->interceptions ? -1 : 1;
  }
}

function sortTeamSPPsCasualties ($a, $b) {
  if ($b->casualties == $a->casualties) {
    return 0;
  } else {
    return $b->casualties < $a->casualties ? -1 : 1;
  }
}

function sortTeamSPPsMVPS ($a, $b) {
  if ($b->mvps == $a->mvps) {
    return 0;
  } else {
    return $b->mvps < $a->mvps ? -1 : 1;
  }
}

function sortTeamSPPsExps ($a, $b) {
  if ($b->exps == $a->exps) {
    return 0;
  } else {
    return $b->exps< $a->exps? -1 : 1;
  }
}

function sortTeamSPPsTotalGame ($a, $b) {
  if ($b->totalGame == $a->totalGame) {
    return 0;
  } else {
    return $b->totalGame < $a->totalGame ? -1 : 1;
  }
}

function sortTeamSPPsTotal ($a, $b) {
  return $b->total - $a->total;
}

function sortTeamSPPsTotalPlayer ($a, $b) {
  if ($b->totalPlayer == $a->totalPlayer) {
    return 0;
  } else {
    return $b->totalPlayer < $a->totalPlayer ? -1 : 1;
  }
}

function sortTeamSPPsTotalPlayerGame ($a, $b) {
  if ($b->totalPlayerGame == $a->totalPlayerGame) {
    return 0;
  } else {
    return $b->totalPlayerGame < $a->totalPlayerGame ? -1 : 1;
  }
}

class TeamSPPs {

  var $completions = 0;
  var $touchdowns = 0;
  var $interceptions = 0;
  var $casualties = 0;
  var $mvps = 0;
  var $exps = 0;
  var $total = 0;
  var $totalGame = 0;
  var $totalPlayer = 0;
  var $totalPlayerGame = 0;
  var $team;

  function TeamSPPs(&$team) {
    $this->team = $team;
    $this->getTeamValues();
  }

  function getRound($value, $div) {
    return round($value / ($div == 0 ? 1 : $div), 2);
  }

  function addPlayer($player) {
    $this->completions += $player->completions;
    $this->touchdowns += $player->tds;
    $this->interceptions += $player->interceptions;
    $this->casualties += $player->casualties;
    $this->mvps += $player->mvps;
    $this->exps += $player->exps;
    $this->total += $player->getSPPs();
  }

  function getGames() {
    $teamId = $this->team->id;
    $rs = executeQuery("select count(1) from match_records where team_id=$teamId");
    if ($row = mysql_fetch_row($rs)) {
      return $row[0];
    }
    return 0;
  }

  function getTeamValues() {

    foreach($this->team->players as $player) {
      $this->addPlayer($player);
    }
    $this->totalGame = $this->total;
    $this->totalPlayer = $this->total;
    $this->totalPlayerGame = $this->total;

    $count = $this->getGames();

    $this->completions = $this->getRound($this->completions, $count);
    $this->touchdowns = $this->getRound($this->touchdowns, $count);
    $this->interceptions = $this->getRound($this->interceptions, $count);
    $this->casualties = $this->getRound($this->casualties, $count);
    $this->mvps = $this->getRound($this->mvps, $count);
    $this->exps = $this->getRound($this->exps, $count);
    $this->totalGame = $this->getRound($this->totalGame, $count);
    $this->totalPlayer = $this->getRound($this->totalPlayer, sizeof($this->team->players));
    $this->totalPlayerGame = $this->getRound($this->totalPlayerGame, $count * sizeof($this->team->players));

  }

  function echoRow($row) {
    echo getEchoTR($row, "center");
    echo "  <td>" . $this->team->getRosterLink()  ."</td>\n";
    echo "  <td>" . getEchoValue($this->team->race->name) . "</td>\n";
    echo "  <td>" . getEchoValue($this->completions) . "</td>\n";
    echo "  <td>" . getEchoValue($this->touchdowns) . "</td>\n";
    echo "  <td>" . getEchoValue($this->interceptions) . "</td>\n";
    echo "  <td>" . getEchoValue($this->casualties) . "</td>\n";
    echo "  <td>" . getEchoValue($this->mvps) . "</td>\n";
    echo "  <td>" . getEchoValue($this->exps) . "</td>\n";
    echo "  <td>" . getEchoValue($this->total) . "</td>\n";
    echo "  <td>" . getEchoValue($this->totalGame) . "</td>\n";
    echo "  <td>" . getEchoValue($this->totalPlayer) . "</td>\n";
    echo "  <td>" . getEchoValue($this->totalPlayerGame) . "</td>\n";
    echo "</tr>\n";
  }
}
?>